package com.kapuscinski.saferoute;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.util.Log;

public class SettingsActivity extends PreferenceActivity {

	private static final String TAG = "SettingsActivity";

	public static final String KEY_FILENAME = "RougnessDataCollectorPreferences";
	public static final String KEY_THRESHOLD_SMALL = "threshold_small";
	public static final String KEY_THRESHOLD_MEDIUM = "threshold_medium";
	public static final String KEY_THRESHOLD_BIG = "threshold_big";
	public static final String KEY_WORK_ON_PAUSE = "work_on_pause";

	public static final float DEFAULT_THRESHOLD_SMALL = 2.5f;
	public static final float DEFAULT_THRESHOLD_MEDIUM = 5f;
	public static final float DEFAULT_THRESHOLD_BIG = 7f;

	private SharedPreferences preferences;
	private EditTextPreference thresholdSmall, thresholdMedium, thresholdBig;
	private CheckBoxPreference workOnPause;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.settings);
		getActionBar().setDisplayHomeAsUpEnabled(true);

		preferences = getSharedPreferences(KEY_FILENAME, Activity.MODE_PRIVATE);
		thresholdSmall = (EditTextPreference) findPreference(KEY_THRESHOLD_SMALL);
		thresholdMedium = (EditTextPreference) findPreference(KEY_THRESHOLD_MEDIUM);
		thresholdBig = (EditTextPreference) findPreference(KEY_THRESHOLD_BIG);
		workOnPause = (CheckBoxPreference) findPreference(KEY_WORK_ON_PAUSE);

		thresholdSmall.setOnPreferenceChangeListener(preferenceListener);
		thresholdMedium.setOnPreferenceChangeListener(preferenceListener);
		thresholdBig.setOnPreferenceChangeListener(preferenceListener);
		workOnPause.setOnPreferenceChangeListener(preferenceListener);

		readPreferences();
	}

	public void readPreferences() {
		thresholdSmall.setDefaultValue(String.valueOf(preferences.getFloat(
				KEY_THRESHOLD_SMALL, DEFAULT_THRESHOLD_SMALL)));
		thresholdMedium.setDefaultValue(String.valueOf(preferences.getFloat(
				KEY_THRESHOLD_MEDIUM, DEFAULT_THRESHOLD_MEDIUM)));
		thresholdBig.setDefaultValue(String.valueOf(preferences.getFloat(
				KEY_THRESHOLD_BIG, DEFAULT_THRESHOLD_MEDIUM)));

		thresholdSmall.setSummary(String.valueOf(preferences.getFloat(
				KEY_THRESHOLD_SMALL, DEFAULT_THRESHOLD_SMALL)));
		thresholdMedium.setSummary(String.valueOf(preferences.getFloat(
				KEY_THRESHOLD_MEDIUM, DEFAULT_THRESHOLD_MEDIUM)));
		thresholdBig.setSummary(String.valueOf(preferences.getFloat(
				KEY_THRESHOLD_BIG, DEFAULT_THRESHOLD_MEDIUM)));

		workOnPause.setChecked(preferences.getBoolean(KEY_WORK_ON_PAUSE, true));
	}

	private OnPreferenceChangeListener preferenceListener = new OnPreferenceChangeListener() {

		@Override
		public boolean onPreferenceChange(Preference preference, Object newValue) {
			SharedPreferences.Editor editor = preferences.edit();
			if (newValue instanceof String) {
				editor.putFloat(preference.getKey(),
						Float.parseFloat((String) newValue));
				preference.setSummary((CharSequence) newValue);
				Log.d(TAG, "Updated " + preference.getTitle() + " with value "
						+ Float.parseFloat((String) newValue));
			} else {
				editor.putBoolean(KEY_WORK_ON_PAUSE, (Boolean) newValue);
				Log.d(TAG, "Saved workOnPause with value: " + String.valueOf((Boolean) newValue));
			}
			editor.commit();
			return true;
		}
	};

}
